package com.sgq.vue.service.picture.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgq.vue.dao.picture.PictureMapper;
import com.sgq.vue.entity.picture.Picture;
import com.sgq.vue.service.picture.PictureDustbinService;
import com.sgq.vue.service.system.FileService;
import com.sgq.vue.vo.query.picture.PictureQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author: 83778
 * @date: 2024-02-21 14:56
 * @desc:
 */
@Service
public class PictureDustbinServiceImpl extends ServiceImpl<PictureMapper, Picture> implements PictureDustbinService {

    @Autowired
    private FileService fileService;

    @Override
    public IPage<Picture> findIsNotRealDeletedByPage(PictureQueryVo queryVo) {
        //记录当前页
        Long current = queryVo.getPageNo();
        queryVo.setPageNo( (current-1) * queryVo.getPageSize());
        List<Picture> pictures = this.baseMapper.selectIsNotRealDeletedByPage(queryVo);
        //使用queryVo查询完成后还原pageNo
        queryVo.setPageNo(current);
        //创建Page 用于返回分页对象
        Page<Picture> page = new Page<>();
        page.setRecords(pictures);
        page.setCurrent(queryVo.getPageNo());
        page.setSize(queryVo.getPageSize());
        //获取数据总数
        Long total = this.baseMapper.selectCountByLike(queryVo);
        page.setTotal(total);
        //获取总页数
        long  pages  = total % queryVo.getPageSize();
        pages = pages == 0 ? pages : total / queryVo.getPageSize() + 1;
        page.setPages(pages);
        return page;
    }

    @Override
    public List<Picture> findIsNotRealDeletedList() {
        return this.baseMapper.selectAllIsNotRealDeleted();
    }

    @Override
    public Integer recover(Long id) {
        return this.baseMapper.recover(id);
    }

    @Override
    public Integer finalDeleteById(Picture picture) {
        //删除oss记录
        fileService.deleteFile(picture.getUrl());

        //删除数据库记录
        Integer integer = this.baseMapper.realDeleteByIds(Arrays.asList(picture.getId()));
        return integer;
    }
}
